<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;

class PermissionController extends Controller {

	public function __construct() {
		$this->middleware('role:system');
	}

	/**
	 * 进入权限列表页面
	 * @return [type] [description]
	 */
	public function getIndex() {
		$listUrl = url('admin/permission');
		session(['listUrl' => $listUrl]);
		$rows = Permission::orderBy('id', 'ASC')->get();
		return view('admin/permission/list', ['rows' => $rows]);
	}

	/**
	 * 进入增加权限页面
	 * @return [type] [description]
	 */
	public function getAdd() {
		return view('admin/permission/add');
	}

	/**
	 * 增加权限
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postInsert(Request $request) {
		$token = session('token');
		if ($token['isHidden'] == false) {
			return $this->warning('Access denied.');
		}

		$name = safe($request->input('name'), 50);
		$display_name = safe($request->input('display_name'));
		$description = safe($request->input('description'), 200);

		if (Permission::where('name', $name)->count()) {
			return $this->warning('权限名称重复');
		}

		$perms = new Permission();
		$perms->name = $name;
		$perms->display_name = $display_name;
		$perms->description = $description;
		$perms->save();

		return redirect(session('listUrl'));
	}

	/**
	 * 进入修改权限页面
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getEdit($id) {
		$perms = Permission::find($id);
		return view('admin/permission/edit', $perms);
	}

	/**
	 * 修改权限
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postUpdate(Request $request) {
		$id = intval($request->input('id'));
		$perms = Permission::find($id);
		if (empty($perms)) {
			return $this->warning('无效的权限');
		}

		if ($request->exists('name')) {
			$name = strval($request->input('name'));
			if ($perms->name != $name) {
				if (Permission::where('name', $name)->count()) {
					return $this->warning('权限名称重复');
				}
				$perms->name = $name;
			}
		}

		if ($request->exists('display_name')) {
			$perms->display_name = safe($request->input('display_name'));
		}
		if ($request->exists('description')) {
			$perms->description = safe($request->input('description'), 200);
		}
		if ($perms->save()) {
			return redirect(session('listUrl'));
		} else {
			return $this->warning('操作失败');
		}
	}

	/**
	 * 删除权限
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postDelete(Request $request) {
		$token = session('token');
		if ($token['isHidden'] == false) {
			return $this->error('Access denied.');
		}

		$id = intval($request->input('id'));
		$perms = Permission::find($id);
		if ($perms->delete()) {
			$perms->roles()->detach();
		}
		return $this->response('ok');
	}

	/**
	 * 进入权限的角色列表
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getRoleList($id) {
		$roles = Role::orderBy('id', 'ASC')->get();

		$perm = Permission::find($id);
		$rows = $perm->roles;
		$cur_role_ids = [];
		foreach ($rows as $row) {
			$cur_role_ids[] = $row->id;
		}

		$data = compact('id', 'perm', 'roles', 'cur_role_ids');
		return view('admin/permission/roles', $data);
	}

}
